package com.itszt.manager.controller;

import com.itszt.common.EasyUITreeBean;
import com.itszt.common.ItemPicUtil;
import com.itszt.manager.VO.EasyUISaveItemBean;
import com.itszt.manager.service.ContentService;
import com.itszt.pojo.TbContent;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ContentController {
    @Resource
    private ContentService contentService;
    @RequestMapping("/content/category/list")
    public List<EasyUITreeBean> showContentCategory(String id){


        List<EasyUITreeBean> easyUITreeBeans=null;
        if (StringUtils.isEmpty(id)) {
            easyUITreeBeans=contentService.showTopNodes();
        }else {
            easyUITreeBeans=contentService.showLeafNodes(id);
        }

        return easyUITreeBeans;

    }
    @RequestMapping("/content/save")
    @ResponseBody
    public EasyUISaveItemBean addContent(TbContent tbContent){
        EasyUISaveItemBean easyUISaveItemBean=new EasyUISaveItemBean();
        tbContent.setPic(ItemPicUtil.genFdfsPath(tbContent.getPic()));
        tbContent.setPic2(ItemPicUtil.genFdfsPath(tbContent.getPic2()));

        try {
            contentService.addContent(tbContent );
            easyUISaveItemBean.setStatus(EasyUISaveItemBean.STATUS_OK);
            easyUISaveItemBean.setMessage("新增内容完毕！ "+tbContent.getTitle());
        } catch (Exception e) {
            e.printStackTrace();
            easyUISaveItemBean.setStatus(EasyUISaveItemBean.STATUS_ERROR);
            easyUISaveItemBean.setMessage("????！ "+tbContent.getTitle());
        }

        return  easyUISaveItemBean;

    }
}
